﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Extra.ERP;

namespace Dictionary
{
    public class xucStateAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_STATE item);

        private readonly IContainer components = null;

        private CalcEdit calPrice;
        private ComboBoxEdit cboUnit;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private LabelControl lcDescription;
        private MemoEdit txtDescription;

        public xucStateAdd()
        {
            InitializeComponent();
            InitData();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_STATE().NewID();
            base.txtNAME.Focus();
        }

        private void AddComboboxEdit(ComboBoxEdit combo, string str)
        {
            combo.Properties.Items.Add(str);
            combo.SelectedIndex = combo.Properties.Items.Count - 1;
        }

        private void cbo_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            xfmUnitAdd.AddedEventHander hander = null;
            if (e.Button.Kind == ButtonPredefines.Plus)
            {
                var edit = sender as ComboBoxEdit;
                string name = edit.Name;
                if ((name != null) && (name == "cboUnit"))
                {
                    var add = new xfmUnitAdd(Common.Class.Actions.Add);
                    if (hander == null)
                    {
                        hander = (s, i) => AddComboboxEdit(cboUnit, i.UnitName);
                    }
                    add.Added += hander;
                    add.ShowDialog();
                }
            }
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private DIC_STATE InitClass()
        {
            return new DIC_STATE
            {
                StateCode = base.txtID.Text,
                StateName = base.txtNAME.Text,
                Price = decimal.Parse(calPrice.EditValue.ToString()),
                Unit = cboUnit.Text,
                Description = txtDescription.Text
            };
        }

        private void InitData()
        {
            new DIC_UNIT().AddComboEdit(cboUnit);
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucStateAdd));
            lcDescription = new LabelControl();
            txtDescription = new MemoEdit();
            cboUnit = new ComboBoxEdit();
            calPrice = new CalcEdit();
            labelControl3 = new LabelControl();
            labelControl4 = new LabelControl();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            cboUnit.Properties.BeginInit();
            calPrice.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(labelControl4);
            base.gcInfo.Controls.Add(labelControl3);
            base.gcInfo.Controls.Add(calPrice);
            base.gcInfo.Controls.Add(cboUnit);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Size = new Size(0x149, 0xc1);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(cboUnit, 0);
            base.gcInfo.Controls.SetChildIndex(calPrice, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl3, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl4, 0);
            base.txtNAME.Location = new Point(0x48, 0x34);
            base.txtNAME.Size = new Size(0xf5, 20);
            base.lblName.Location = new Point(7, 0x37);
            base.txtID.Location = new Point(0x48, 0x1a);
            base.txtID.Size = new Size(0xf5, 20);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnSave.Location = new Point(0x20, 0xce);
            base.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnCancel.Location = new Point(0xdf, 0xce);
            base.btnSaveNew.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            base.btnSaveNew.Location = new Point(130, 0xce);
            lcDescription.Location = new Point(8, 0x8a);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 14;
            lcDescription.Text = "Ghi ch\x00fa:";
            lcDescription.Click += labelControl1_Click;
            txtDescription.Location = new Point(0x48, 0x8a);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xf5, 0x2b);
            txtDescription.TabIndex = 13;
            txtDescription.TabStop = false;
            cboUnit.Location = new Point(0x48, 0x6c);
            cboUnit.Name = "cboUnit";
            cboUnit.Properties.Buttons.AddRange(new[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            cboUnit.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cboUnit.Size = new Size(0xf5, 20);
            cboUnit.TabIndex = 0x12;
            cboUnit.ButtonClick += cbo_ButtonClick;
            calPrice.Location = new Point(0x48, 0x4f);
            calPrice.Name = "calPrice";
            calPrice.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calPrice.Properties.Mask.UseMaskAsDisplayFormat = true;
            calPrice.Size = new Size(0xf5, 20);
            calPrice.TabIndex = 0x15;
            labelControl3.Location = new Point(8, 0x52);
            labelControl3.Name = "labelControl3";
            labelControl3.Size = new Size(50, 13);
            labelControl3.TabIndex = 0x16;
            labelControl3.Text = "Tiền c\x00f4ng:";
            labelControl4.Location = new Point(9, 0x6f);
            labelControl4.Name = "labelControl4";
            labelControl4.Size = new Size(0x38, 13);
            labelControl4.TabIndex = 0x17;
            labelControl4.Text = "Đơn vị t\x00ednh:";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucStateAdd";
            base.Size = new Size(330, 0xf2);
            base.Load += new EventHandler(xucStateAdd_Load);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            cboUnit.Properties.EndInit();
            calPrice.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {
        }

        private void RaiseSuccessEventHander(DIC_STATE item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_STATE item)
        {
            base.txtID.Text = item.StateCode;
            SYS_LOG.Insert("Danh Mục C\x00f4ng Đoạn", "Xem", base.txtID.Text);
            if (base.m_Status == Common.Class.Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.StateName;
            calPrice.EditValue = item.Price;
            cboUnit.Text = item.Unit;
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Common.Class.Actions.Add)
            {
                var dic_state = new DIC_STATE();
                if (dic_state.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_state = new DIC_STATE();
                if ((base.m_Status == Common.Class.Actions.Add) && dic_state.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_STATE
            {
                StateCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            SYS_LOG.Insert("Danh Mục C\x00f4ng Đoạn", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            DIC_STATE item = InitClass();
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            SYS_LOG.Insert("Danh Mục C\x00f4ng Đoạn", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            DIC_STATE item = InitClass();
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }

        private void xucStateAdd_Load(object sender, EventArgs e)
        {
        }
    }
}